﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Platform;
using System.IO;
using PlanWater.Helper.Entity;

namespace PlanWater.Helper 
{


    /// <summary>
    /// 配置帮助 保持单例
    /// </summary>
    public class ConfigHelper
    {

        #region 变量属性
        public static readonly ConfigHelper instance = new ConfigHelper();
        private Config _configInstance;
        private static object _locker = new object(); 
        public Config ConfigInstance {
            get {
                lock (_locker)
                {
                    if (null == _configInstance)
                    {
                        var value = CreateConfigCache();
                        string msg = string.Empty;
                        if (FilePathCheck.CheckConfig(value, out msg))
                        {
                            _configInstance = value;
                        }
                        else
                        {
                            LogHelper.Instance.Log(msg);    
                        }
                    }
                    return _configInstance;
                }
                }
        }
        #endregion

        #region 初始化
        private ConfigHelper() { }
        #endregion

        /// <summary>
        /// 创建配置缓存
        /// </summary>
        /// <returns></returns>
        public Config CreateConfigCache()
        {
            Config entity = new Config();
            entity.QRCodeURL = ReplaceConfigStr(ConfigurationManager.AppSettings["QRCodeURL"].ToString());
            entity.BarCodeURL = ReplaceConfigStr(ConfigurationManager.AppSettings["BarCodeURL"].ToString());
            entity.ApprovalCodeURL = ReplaceConfigStr(ConfigurationManager.AppSettings["ApprovalCodeURL"].ToString());
            int _timer =1000;
            Int32.TryParse(ReplaceConfigStr(ConfigurationManager.AppSettings["TriggerCycle"].ToString()), out _timer);
            entity.TriggerCycle = _timer;
            entity.SaveQRCodeImgPath = ReplaceConfigStr(ConfigurationManager.AppSettings["SaveQRCodeImgPath"].ToString());
            entity.SaveBarCodeImgPath =ReplaceConfigStr( ConfigurationManager.AppSettings["SaveBarCodeImgPath"].ToString());
            entity.SQLDataSource = ReplaceConfigStr(ConfigurationManager.AppSettings["SQLDataSource"].ToString());
            return entity;
        }

        /// <summary>
        /// 替换字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string ReplaceConfigStr(string value) 
        {
            string newValue = value;
            if (newValue.Contains("@@")) 
            {
               newValue= newValue.Replace("@@", "&");
            }
            return newValue;
        }
    
    }

    
    public class FilePathCheck {
        /// <summary>
        /// 验证Config配置
        /// </summary>
        /// <param name="config"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool CheckConfig(Config config,out string message)
        {
         try{  
             message=string.Empty;
            if (null == config)
                throw new Exception("配置文件对象不存在！"); 
            if(!Directory.Exists(config.SaveBarCodeImgPath))
                throw new Exception("配置文件条形码图片路径文件夹不存在！"); 
            if(!Directory.Exists(config.SaveQRCodeImgPath))
                throw  new Exception("配置文件二维码图片路径文件夹不存在！");
             }
            catch(Exception e)
         {
               message=e.ToString();
               return false;
          }
            return true;
        }
    }
}
